package com.ruoyi.web.service.impl;

import java.sql.Wrapper;
import java.util.List;
import java.util.Random;
import java.util.UUID;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.domain.Product;
import com.ruoyi.web.mapper.ProductMapper;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.mapper.ProductShopMapper;
import com.ruoyi.web.domain.ProductShop;
import com.ruoyi.web.service.IProductShopService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品-店铺关联Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-31
 */
@Service
public class ProductShopServiceImpl extends ServiceImpl<ProductShopMapper,ProductShop> implements IProductShopService
{
    @Autowired
    private ProductShopMapper productShopMapper;

    @Autowired
    private ProductMapper productMapper;

    /**
     * 查询商品-店铺关联
     * 
     * @param id 商品-店铺关联主键
     * @return 商品-店铺关联
     */
    @Override
    public ProductShop selectProductShopById(Long id)
    {
        return productShopMapper.selectProductShopById(id);
    }

    /**
     * 查询商品-店铺关联列表
     * 
     * @param productShop 商品-店铺关联
     * @return 商品-店铺关联
     */
    @Override
    public List<ProductShop> selectProductShopList(ProductShop productShop)
    {
        return productShopMapper.selectProductShopList(productShop);
    }

    /**
     * 新增商品-店铺关联
     * 
     * @param productShop 商品-店铺关联
     * @return 结果
     */
    @Override
    @Transactional
    public AjaxResult insertProductShop(ProductShop productShop)
    {
        String name = productShop.getProductName();
        if (StringUtils.isEmpty(name)){
            return AjaxResult.error("必要信息未填");
        }

        //商品ID
        Long productId = Math.abs(new Random().nextLong());
        //店铺ID
        String shopId = String.valueOf(UUID.randomUUID());

        productShop.setProductId(productId);
        productShop.setShopId(shopId);

        Product product = new Product();
        BeanUtils.copyProperties(productShop,product);
        productMapper.insertProduct(product);
        productShop.setCreateTime(DateUtils.getNowDate());
        return AjaxResult.success(productShopMapper.insertProductShop(productShop));
    }

    /**
     * 修改商品-店铺关联
     * 
     * @param productShop 商品-店铺关联
     * @return 结果
     */
    @Override
    public int updateProductShop(ProductShop productShop)
    {
        productShop.setUpdateTime(DateUtils.getNowDate());
        Product product = new Product();
        BeanUtils.copyProperties(productShop,product);
        productMapper.updateProduct(product);
        return productShopMapper.updateProductShop(productShop);
    }

    /**
     * 批量删除商品-店铺关联
     * 
     * @param ids 需要删除的商品-店铺关联主键
     * @return 结果
     */
    @Override
    public int deleteProductShopByIds(Long[] ids)
    {
        return productShopMapper.deleteProductShopByIds(ids);
    }

    /**
     * 删除商品-店铺关联信息
     * 
     * @param id 商品-店铺关联主键
     * @return 结果
     */
    @Override
    public int deleteProductShopById(Long id)
    {
        return productShopMapper.deleteProductShopById(id);
    }
}
